using System.Collections.Generic;
using Microsoft.Web.Management.Client;
using Microsoft.Web.Management.Client.Extensions;
using Microsoft.Web.Management.Client.Win32;

namespace LeastPrivilege.CustomBasicAuthentication.Management
{
    // this controls the logic of our UI module from within the authentication section
    class CustomBasicAuthenticationFeature : AuthenticationFeature
    {
        private bool _isLocked;
        private TaskList _taskList;
        private CustomBasicAuthenticationModuleProxy _serviceProxy;

        public CustomBasicAuthenticationFeature(Module module)
            : base(module)
        { }

        public override AuthenticationType AuthenticationType
        {
            get { return AuthenticationType.ChallengeBased; }
        }

        public bool IsLocked
        {
            get { return _isLocked; }
        }

        // returns the task list with toggle/edit links
        public override TaskList GetTaskList()
        {
            if (_taskList == null)
            {
                this._taskList = new CustomBasicAuthenticationTaskList(this);
            }

            return this._taskList;
        }

        public override string Name
        {
            get { return "Custom Basic Authentication"; }
        }

        // loads enabled and locked status and updates UI
        public override void Load()
        {
            using (WaitCursor cursor = new WaitCursor())
            {
                Dictionary<string, string> settings = this.ServiceProxy.GetEnabled();

                base.SetEnabled(bool.Parse(settings["enabled"]));
                this._isLocked = bool.Parse(settings["isLocked"]);
            }
        }

        // returns the service proxy to talk to the server service
        private CustomBasicAuthenticationModuleProxy ServiceProxy
        {
            get
            {
                if (this._serviceProxy == null)
                {
                    Connection connection = (Connection)base.GetService(typeof(Connection));
                    this._serviceProxy = (CustomBasicAuthenticationModuleProxy)
                        connection.CreateProxy(base.Module, typeof(CustomBasicAuthenticationModuleProxy));
                }

                return this._serviceProxy;
            }
        }

        // shows the configuration dialog
        internal void EditSettings()
        {
            Dictionary<string, string> settings = null;

            using (WaitCursor cursor = new WaitCursor())
            {
                settings = this.ServiceProxy.GetConfiguration();
            }
        
            CustomBasicAuthenticationSettingsDialog dialog = new CustomBasicAuthenticationSettingsDialog(
                base.Module, 
                this.ServiceProxy, 
                this._isLocked, settings);

            ((IManagementUIService)base.GetService(
                typeof(IManagementUIService))).ShowDialog(dialog);

        }

        // toggles the enabled status and saves config
        internal void ToggleEnabled()
        {
            bool enabled = !base.IsEnabled;

            using (WaitCursor cursor = new WaitCursor())
            {
                this.ServiceProxy.SetEnabled(enabled);
                base.SetEnabled(enabled);
            }
            base.OnAuthenticationSettingsSaved();
        }
    }
}
